package com.ruoyi.odorservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.odorservice.entity.MaterialOdor;
import com.ruoyi.odorservice.entity.Odor;
import com.ruoyi.odorservice.mapper.OdorMapper;
import com.ruoyi.odorservice.model.bean.OdorInfoBean;
import com.ruoyi.odorservice.model.vo.OdorMaterialVo;
import com.ruoyi.odorservice.service.OdorService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class OdorServiceImpl extends ServiceImpl<OdorMapper, Odor> implements OdorService {
    private OdorMapper odorMapper;
    @Override
    public List<Odor> searchOdorList(String key) {
        LambdaQueryWrapper<Odor> wrapper = new LambdaQueryWrapper<Odor>()
                .select(Odor::getId, Odor::getOdorName)
                .like(StringUtils.isNotEmpty(key), Odor::getOdorName, key)
                .last("limit 5");
        return list(wrapper);
    }

    @Override
    public List<OdorMaterialVo> getOdorList() {
        // 获取所有气味的ID
        LambdaQueryWrapper<Odor> wrapper = new LambdaQueryWrapper<Odor>()
                .select(Odor::getId, Odor::getOdorName, Odor::getRemark);
        List<Odor> odorList = list(wrapper);
        List<Integer> ids = odorList.stream().map(Odor::getId).collect(Collectors.toList());

        // 获取所有气味的详细信息，包括没有关联物质的气味
        List<OdorMaterialVo> allOdorMaterialVoList = odorList.stream().map(odor -> {
            OdorMaterialVo vo = new OdorMaterialVo();
            vo.setOdorId(odor.getId());
            vo.setOdorName(odor.getOdorName());
            vo.setRemark(odor.getRemark());
            vo.setMaterialList(new ArrayList<>());
            return vo;
        }).collect(Collectors.toList());

        // 获取有关联物质的气味信息
        List<OdorMaterialVo> odorMaterialVoList = odorMapper.selectOdorListByIds(ids);

        // 合并数据
        Map<Integer, OdorMaterialVo> odorMaterialMap = allOdorMaterialVoList.stream()
                .collect(Collectors.toMap(OdorMaterialVo::getOdorId, vo -> vo));

        odorMaterialVoList.forEach(vo -> {
            if (odorMaterialMap.containsKey(vo.getOdorId())) {
                odorMaterialMap.get(vo.getOdorId()).setMaterialList(vo.getMaterialList());
            } else {
                odorMaterialMap.put(vo.getOdorId(), vo);
            }
        });

        // 返回合并后的结果列表
        return new ArrayList<>(odorMaterialMap.values());
    }

    @Override
    @Transactional
    public Boolean createOdor(OdorInfoBean odorInfoBean) {
        Odor odor = new Odor();
        BeanUtils.copyProperties(odorInfoBean, odor);
        int saveState = odorMapper.insert(odor);
        if(saveState == 0){
            return false;
        }

        boolean createState = true;
        String materials = odorInfoBean.getMaterialIds();
        if (StringUtils.isNotEmpty(materials)){
            List<MaterialOdor> materialOdorList = new ArrayList<>();
            String[] materialIds = materials.split(",");
            for(String materialId : materialIds){
                MaterialOdor materialOdor = new MaterialOdor(Integer.parseInt(materialId.trim()), odor.getId());
                materialOdorList.add(materialOdor);
            }
            int insertState = this.baseMapper.insertBatchMaterialOdor(materialOdorList);
            if(insertState == 0){
                createState = false;
            }
        }
        return createState;
    }

    @Override
    @Transactional
    public Boolean updateOdor(OdorInfoBean odorInfoBean) {
        if(odorInfoBean == null){
            return false;
        }
        Odor odor = new Odor();
        BeanUtils.copyProperties(odorInfoBean, odor);
        odor.setId(odorInfoBean.getOdorId());
        boolean updateStatus = updateById(odor);
        if(!updateStatus){
            return false;
        }
        int deleteState = this.baseMapper.deleteMaterialOdorByOdorId(odorInfoBean.getOdorId());
        if(deleteState == 0){
            return false;
        }
        boolean createState = true;
        String materials = odorInfoBean.getMaterialIds();
        if (StringUtils.isNotEmpty(materials)){
            List<MaterialOdor> materialOdorList = new ArrayList<>();
            String[] materialIds = materials.split(",");
            for(String materialId : materialIds){
                MaterialOdor materialOdor = new MaterialOdor(Integer.parseInt(materialId.trim()), odor.getId());
                materialOdorList.add(materialOdor);
            }
            int insertState = this.baseMapper.insertBatchMaterialOdor(materialOdorList);
            if(insertState == 0){
                createState = false;
            }
        }
        return createState;
    }

    @Override
    @Transactional
    public Boolean deleteOdor(Integer id) {
        int deleteMaterialOdor = this.baseMapper.deleteMaterialOdorByOdorId(id);
        int deleteOdorNum = this.baseMapper.deleteById(id);
        return deleteMaterialOdor != 0 && deleteOdorNum != 0;
    }
}
